#手机号校验
import re


def validate_phone_number(phone):
    pattern = r'^1[3-9]\d{9}$'
    return bool(re.match(pattern, phone))


# 示例
print(validate_phone_number('13800138000'))  # True


#身份证号校验
def validate_id_card(id_card):
    pattern = r'(^\d{15}$)|(^\d{17}([0-9]|X)$)'
    return bool(re.match(pattern, id_card))


# 示例
print(validate_id_card('11010519491231002X'))  # True




#1、获取当前日期和时间
from datetime import datetime
now = datetime.now()
print("当前时间:", now)
#格式化输出当前时间
from datetime import datetime
#2. 格式化输出当前时间
formatted_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
print("格式化后的当前时间:", formatted_time)
#3、计算两个日期之间的天数差异
from datetime import datetime
date1 = datetime(2025, 5, 1)
date2 = datetime(2025, 6, 1)
difference = (date2 - date1).days
print(f"{date1} 和 {date2} 之间相差 {difference} 天")
#4、将字符串转换为日期对象
from datetime import datetime
date_str = "2025-05-01"
date_obj = datetime.strptime(date_str, "%Y-%m-%d")
print("日期对象:", date_obj)

#5. 将日期对象转换为字符串
from datetime import datetime
date_obj = datetime.now()
date_str = date_obj.strftime("%Y-%m-%d")
print("日期字符串:", date_str)
#6. 获取今天的日期
from datetime import date
today = date.today()
print("今天的日期:", today)
#7. 添加一天到当前日期
from datetime import datetime, timedelta
now = datetime.now()
tomorrow = now + timedelta(days=1)
print("明天的日期:", tomorrow)
#8. 减去一小时从当前时间
from datetime import datetime, timedelta
an_hour_ago = datetime.now() - timedelta(hours=1)
print("一小时前的时间:", an_hour_ago)
#9. 判断是否是闰年
import calendar
year = 2024
if calendar.isleap(year):
    print(f"{year} 是闰年")
else:
    print(f"{year} 不是闰年")
#10. 获取本月的第一天和最后一天
from datetime import datetime, timedelta
first_day = datetime.today().replace(day=1)
last_day = (first_day + timedelta(days=32)).replace(day=1) - timedelta(days=1)
print("本月第一天:", first_day.date())
print("本月最后一天:", last_day.date())
#11. 创建定时器
import time
def countdown(t):
    while t:
        mins, secs = divmod(t, 60)
        timer = '{:02d}:{:02d}'.format(mins, secs)
        print(timer, end="\r")
        time.sleep(1)
        t -= 1
countdown(5)
print("计时结束")
#12. 设置定时任务（使用 sched 模块）
import sched, time
scheduler = sched.scheduler(time.time, time.sleep)
def print_event(name):
    print(f"事件: {name} 在 {time.time()} 执行")
print('开始:', time.time())
scheduler.enter(2, 1, print_event, ('事件1',))
scheduler.run()
#13. 获取指定月份的日历
import calendar
year = 2025
month = 5
print(calendar.month(year, month))
#14. 时间戳转日期时间
import time
from datetime import datetime
timestamp = time.time()
print("时间戳:", timestamp)
date_time = datetime.fromtimestamp(timestamp)
print("日期时间:", date_time)
#15. 日期时间转时间戳
from datetime import datetime
date_time_str = '2025-05-05 12:00:00'
date_time_obj = datetime.strptime(date_time_str, '%Y-%m-%d %H:%M:%S')
timestamp = date_time_obj.timestamp()
print("时间戳:", timestamp)
#16. 周几计算
from datetime import datetime
date = datetime(2025, 5, 5)
print(date.strftime("%A"))  # 输出星期几‍
#17. 计算给定日期之后的n个工作日（排除周末）
from datetime import datetime, timedelta
def add_working_days(start_date, days):
    current_date = start_date
    added_days = 0
    while added_days < days:
        current_date += timedelta(days=1)
        if current_date.weekday() < 5:  # 排除周末
            added_days += 1
    return current_date
start_date = datetime(2025, 5, 5)
print(add_working_days(start_date, 10))  # 10个工作日后的日期‍
#18. 计算两个日期之间的周数差异
from datetime import datetime
date1 = datetime(2025, 5, 1)
date2 = datetime(2025, 6, 1)
weeks_difference = (date2 - date1).days // 7
print(f"{date1} 和 {date2} 之间相差 {weeks_difference} 周")
#19. 获取当前星期的周一和周日
from datetime import datetime, timedelta
today = datetime.today()
start_of_week = today - timedelta(days=today.weekday())
end_of_week = start_of_week + timedelta(days=6)
print("本周周一:", start_of_week.date())
print("本周周日:", end_of_week.date())
#20. 使用 time.sleep() 实现简单的延迟
import time
print("开始")
time.sleep(2)  # 等待2秒
print("2秒后...")